/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.business.sys.controller.RoleController.java 
 * <p>
 * 类产生时间: 2014-5-12 下午 15:43
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.SysRole;
import cn.com.oceansoft.business.sys.service.ISysResourceService;
import cn.com.oceansoft.business.sys.service.ISysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.*;

/**
 * Description
 *
 * @author: chenw
 * @time: 2014-5-12 下午 15:43
 */
@Controller
@RequestMapping("sys/role")
public class SysRoleController extends BaseController {

    @Resource
    private ISysRoleService roleService;

    @Resource
    private ISysResourceService resourceService;

    /**
     * 进入系统角色管理首页
     *
     * @return String
     */
    @RequestMapping(value = "index", method = GET)
    public String openRoleIndex(Query query, Model model) {
        query.setOrderField("sort");
        query.setOrderDirection("asc");
        model.addAttribute("query", query);
        model.addAttribute("pager", roleService.findPageByCondition(query));
        log(String.format("查看系统角色%d页", query.getPageNum()));
        return "sys/sys-role-list";
    }

    /**
     * 进入系统角色管理首页
     *
     * @return String
     */
    @RequestMapping(value = "list", method = POST)
    public String loadRoleByPage(Query query, Model model) {
        query.setOrderField("sort");
        query.setOrderDirection("asc");
        model.addAttribute("query", query);
        model.addAttribute("pager", roleService.findPageByCondition(query));
        log(String.format("查看系统角色列表第%d页", query.getPageNum()));
        return "sys/sys-role-list";
    }

    /**
     * 角色新增
     *
     * @return String
     */
    @RequestMapping(value = "new", method = GET)
    public String openAddRole() {
        return "sys/sys-role-new";
    }

    /**
     * 打开编辑页面
     *
     * @return String
     */
    @RequestMapping(value = "view/{role_id}", method = GET)
    public String viewRole(@PathVariable("role_id") int roleId, Model model) {
        model.addAttribute("role", roleService.findById(roleId));
        return "sys/sys-role-edit";
    }

    /**
     * 角色新增
     *
     * @param role 新增角色实体对象
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addRole(SysRole role) {
        log(String.format("新增角色%s", role.getName()));
        return roleService.save(role);
    }

    /**
     * 删除角色信息
     *
     * @param roleId 角色ID
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "{role_id}", method = DELETE)
    public Result deleteRole(@PathVariable("role_id") int roleId) {
        Result rt = roleService.deleteRoleById(roleId);
        log(String.format("删除编号为%d角色%s", roleId, rt.isSucc() ? "成功" : "失败"));
        return rt;
    }

    /**
     * 角色编辑
     *
     * @param role 新角色实体对象
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editRole(SysRole role) {
        return roleService.update(role);
    }
}
